/**
 * 演示程序当前的 “注册/登录” 等操作，是基于 “本地存储” 完成的
 * 当您要参考这个演示程序进行相关 app 的开发时，
 * 请注意将相关方法调整成 “基于服务端Service” 的实现。
 **/
var api = 'http://app.chinajci.com/sms/apply.aspx';
var uuid = '';
var request = {};
document.title = '中国汇易网';

(function($, owner) {	
	/*
	 返回结果处理
	 * */
	owner.init = function(){
		uuid = owner.getUID();
		state = owner.getState();
		request = owner.getRequest();
	};	
	
	owner.checkLogin = function(){
		if(owner.getState().name=='' || owner.getState().name==undefined){
			return false;
		}else{
			return true;
		}
	}
	
	owner.getState = function() {
		var stateText = localStorage.getItem('$state') || "{}";
		return JSON.parse(stateText);
	};

	/**
	 * 设置当前状态
	 **/
	owner.setState = function(state) {
		state = state || {};
		localStorage.setItem('$state', JSON.stringify(state));
	};

	var checkMobile = function(mobile) {		
		var strRegex = "^1[3|4|5|7|8|9][0-9]{9}$";
		var re = new RegExp(strRegex);
		if (re.test(mobile)) {
			return true;
		} else {
			return false;
		}
	};

	/**
	 * 设置应用本地配置
	 **/
	owner.getUID = function() {
		var uidText = localStorage.getItem('$uuid') || "";
		if(uidText==''){
			uidText = owner.randomString(32);
			localStorage.setItem('$uuid', uidText);
		}
		return uidText;
	};
	
	/**
	 * 获取本地是否安装客户端
	 **/
	owner.randomString = function(len) {
		len = len || 32;
	　　	var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; 
	　　	var maxPos = $chars.length;
	　　	var str = '';
	　　	for (i = 0; i < len; i++) {
	　　　	str += $chars.charAt(Math.floor(Math.random() * maxPos));
	　　	}
	　　	return str;
	};

	/**
	 * 获取url参数
	 **/
	owner.getRequest = function() {
		url = location.search; //获取url中"?"符后的字串   
		theRequest = new Object();   
		if (url.indexOf("?") != -1) {   
			var str = url.substr(1);   
		  	strs = str.split("&");   
		  	for(var i = 0; i < strs.length; i ++) {   
		    	theRequest[strs[i].split("=")[0]]=decodeURI(strs[i].split("=")[1]);   
		  	}   
		}   
		return theRequest;
	};

	owner.getKeyword = function() {
		var keyword = localStorage.getItem('$keyword');
		return keyword;
	};	

	owner.setKeyword = function(keyword) {
		keys = owner.getKeyword();
		if (keys=='' || keys==undefined){
			localStorage.setItem('$keyword', keyword);
		}else{
			var result=false;
			arr = keys.split("|")
			for (var i = 0; i < arr.length; i++) {
				if(arr[i]==keyword){
                    result = true;
                }
			}
            if(!result){
                localStorage.setItem('$keyword', keys+'|'+keyword);
            }
		}
	};
}(mui, window.app = {}));